package com.donfu.web.controller.house;

import java.util.ArrayList;
import java.util.List;

import io.swagger.annotations.Api;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.donfu.common.annotation.Log;
import com.donfu.common.core.controller.BaseController;
import com.donfu.common.core.domain.AjaxResult;
import com.donfu.common.enums.BusinessType;
import com.donfu.system.domain.house.HouseComment;
import com.donfu.system.service.house.IHouseCommentService;
import com.donfu.common.utils.poi.ExcelUtil;
import com.donfu.common.core.page.TableDataInfo;

/**
 * 评价信息Controller
 *
 * @author mo
 * @date 2021-12-01
 */
@Api(tags = "后台-评价管理")
@RestController
@RequestMapping("/house/comment")
public class HouseCommentController extends BaseController {
    @Autowired
    private IHouseCommentService houseCommentService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询评价信息列表
     */
    @PreAuthorize("@ss.hasPermi('house:comment:list')")
    @GetMapping("/list")
    public TableDataInfo list(HouseComment houseComment) {
        startPage();
        List<HouseComment> houseComments = houseCommentService.selectHouseCommentList(houseComment);
       /* for (int i = 0; i < houseComments.size(); i++) {
            //评论信息存入redis
            redisTemplate.opsForValue().set("comment:" + houseComments.get(i).getId(), houseComments.get(i));
        }*/
        //二级评论
        if (houseComments == null || houseComments.size() == 0) {
            return null;
        }
        List<HouseComment> parents = new ArrayList<>();
        List<HouseComment> tows = new ArrayList<>();
        for (HouseComment comment : houseComments) {
            if (comment.getParentId() == null) {
                //一级评论集合
                parents.add(comment);
            }
        }
        // 遍历一级评论集合
        for (HouseComment parent : parents) {
            // 拿到所有评论, 然后与一级评论相比较， 拿到所有一级评论的子评论
            for (HouseComment comment : houseComments) {
                if (comment.getParentId() != null) {
                    if (comment.getParentId() == parent.getId().intValue()) {
                        if (parent.getChildren() == null) {
                            parent.setChildren(new ArrayList<>());
                        }
                        // 将二级存入一级子集
                        parent.getChildren().add(comment);
                        tows.add(comment);
                    }
                }

            }
        }
        for (HouseComment tow : tows) {
            // 拿到所有评论, 然后与二级评论相比较， 拿到所有二级评论的子评论
            for (HouseComment comment : houseComments) {
                if (comment.getParentId() != null) {
                    if (comment.getParentId() == tow.getId().intValue()) {
                        if (tow.getChildren() == null) {
                            tow.setChildren(new ArrayList<>());
                        }
                        // 将三级存入二级子集
                        tow.getChildren().add(comment);
                    }
                }

            }
        }
        return getDataTable(parents);
    }


    /**
     * 导出评价信息列表
     */
    @PreAuthorize("@ss.hasPermi('house:comment:export')")
    @Log(title = "评价信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(HouseComment houseComment) {
        List<HouseComment> list = houseCommentService.selectHouseEvaluateList(houseComment);
        ExcelUtil<HouseComment> util = new ExcelUtil<HouseComment>(HouseComment.class);
        return util.exportExcel(list, "评价信息数据");
    }

    /**
     * 获取评价信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('house:comment:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        HouseComment houseComment = houseCommentService.selectHouseCommentById(id);
        if (houseComment!=null) {
            HouseComment c = new HouseComment();
            List<HouseComment> houseComments = houseCommentService.selectHouseCommentList(c);
            List<HouseComment> parents = new ArrayList<>();
            for (HouseComment comment : houseComments) {
                if (comment.getParentId() != null) {
                    //存放二级评论
                    if (comment.getParentId() == houseComment.getId().intValue()) {
                        parents.add(comment);
                    }
                }
            }
            //子评论
            for (HouseComment parent : parents) {
                for (HouseComment comment : houseComments) {
                    if (comment.getParentId() != null) {
                        if (comment.getParentId() == parent.getId().intValue()) {
                            if (parent.getChildren() == null) {
                                parent.setChildren(new ArrayList<>());
                            }
                            //把三级评论存到二级评论
                            parent.getChildren().add(comment);
                        }
                    }
                }
            }
            houseComment.setChildren(parents);
        }
        return AjaxResult.success(houseComment);
    }

    /* *//**
     * 新增评价信息
     *//*
    @PreAuthorize("@ss.hasPermi('house:comment:add')")
    @Log(title = "评价信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HouseComment houseComment)
    {
        return toAjax(houseCommentService.insertHouseComment(houseComment));
    }*/

    /**
     * 修改评价信息
     */
    @PreAuthorize("@ss.hasPermi('house:comment:edit')")
    @Log(title = "评价信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HouseComment houseComment) {
        return toAjax(houseCommentService.updateHouseComment(houseComment));
    }

    /**
     * 删除评价信息
     */
    @PreAuthorize("@ss.hasPermi('house:comment:remove')")
    @Log(title = "评价信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(houseCommentService.deleteHouseCommentByIds(ids));
    }
}
